package com.cbx.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cbx.dao.Userdao;
import com.dao.ObjectDao;
import com.pro.bean.Info;
import com.pro.bean.Mark;
import com.pro.bean.Users;

public class AddCardServlet extends HttpServlet {

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		SimpleDateFormat sDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String txtComJNam = request.getParameter("txtComJNam");
		String txtComJNampy = request.getParameter("txtComJNampy");
		String txtComJNameng = request.getParameter("txtComJNameng");
		String txtComunit = request.getParameter("txtComunit");
		String txtComcaree = request.getParameter("txtComcaree");
		String txtComname = request.getParameter("txtComname");
		System.out.print("ssss"+txtComname);
		String txtComnamepy = request.getParameter("txtComnamepy");
		String txtCommycard = request.getParameter("txtCommycard");

		String zip1 = request.getParameter("zip1");
		String zipprovince1 = request.getParameter("zipprovince1");
		String zipcity1 = request.getParameter("zipcity1");
		String zipzone1 = request.getParameter("zipzone1");
		String zipaddress1 = request.getParameter("zipaddress1");
		String zipphone1 = request.getParameter("zipphone1");
		String zipphone2 = request.getParameter("zipphone2");
		String zipmobile1 = request.getParameter("zipmobile1");
		String zipfax1 = request.getParameter("zipfax1");
		String zipemail1 = request.getParameter("zipemail1");
		String zipurl1 = request.getParameter("zipurl1");

		String zip2 = request.getParameter("zip2");
		String zipprovince2 = request.getParameter("zipprovince2");
		String zipcity2 = request.getParameter("zipcity2");
		String zipzone2 = request.getParameter("zipzone2");
		String zipaddress2 = request.getParameter("zipaddress2");
		String ziptelphone1 = request.getParameter("ziptelphone1");
		String ziptelphone2 = request.getParameter("ziptelphone2");
		String zipmobile2 = request.getParameter("zipmobile2");
		String zipfax2 = request.getParameter("zipfax2");
		String zipemail2 = request.getParameter("zipemail2");
		String zipurl2 = request.getParameter("zipurl2");

		System.out.println(request.getParameter("ddlstPublic"));
		int ddlstPublic = Integer.parseInt(request.getParameter("ddlstPublic"));
		int ddlstImptantKub = Integer.parseInt(request
				.getParameter("ddlstImptantKub"));
		String ddlstRelationKub = request.getParameter("ddlstRelationKub");
		String ddlstJobKub = request.getParameter("ddlstJobKub");
		String img1 = request.getParameter("img1");
		String img2 = request.getParameter("img2");

		String ddlstSexKub = request.getParameter("ddlstSexKub");
		String ddlstBlodKub = request.getParameter("ddlstBlodKub");
		String birthday = request.getParameter("birthday");
		String txtMemo = request.getParameter("txtMemo");

		String industry = request.getParameter("industry");
		String establish = request.getParameter("establish");
		int funding = 0;
		float staff=0.0f;
		if(request.getParameter("funding")!=null)
			try{
			funding = Integer.parseInt(request.getParameter("funding"));
			}catch(NumberFormatException e)
			{
				;
			}
		if(request.getParameter("staff")!=null)
			try{
			staff = Float.parseFloat(request.getParameter("staff"));
			}catch(NumberFormatException e)
			{
				;
			}
		String security = request.getParameter("security");
		String corporate = request.getParameter("corporate");
		int mode = Integer.parseInt(request.getParameter("mode"));
		int upid = 0;
		if(mode != 0)
			upid = Integer.parseInt(request.getParameter("cardid"));
		Info info = new Info();
		info.setAddress1(zipaddress1);
		info.setAddress2(zipaddress2);
		info.setBirthday(birthday);
		info.setBlood(ddlstBlodKub);
		info.setBondCode(security);
		info.setBusiness(industry);
		info.setCapital(funding);
		info.setCity1(zipcity1);
		info.setCity2(zipcity2);
		info.setCmp(txtComJNam);
		info.setCmpEng(txtComJNameng);
		info.setCmpFdDate(establish);
		info.setCmpPinYin(txtComJNampy);

		info.setCrtdate(new Date(new java.util.Date().getTime()));
		info.setUptime(new Date(new java.util.Date().getTime()));

		info.setDept(txtComunit);
		info.setEmail1(zipemail1);
		info.setEmail2(zipemail2);
		info.setEmpNum(staff);
		info.setFax1(zipfax1);
		info.setFax2(zipfax2);
		info.setFirstName(txtComname);
		info.setFirstNamePY(txtComnamepy);
		info.setImportant(ddlstImptantKub);
		info.setJob(ddlstJobKub);
		info.setMemo(txtMemo);
		info.setMobileTel1(zipmobile1);
		info.setMobileTel2(zipmobile2);
		info.setPost(txtComcaree);
		info.setProvince1(zipprovince1);
		info.setProvince2(zipprovince2);
		info.setRegion1(zipzone2);
		info.setRegion2(zipzone1);
		info.setRelation(ddlstRelationKub);
		info.setRepName(corporate);

		info.setSex(ddlstSexKub);
		info.setTelFirst1(zipphone1);
		info.setTelFirst2(ziptelphone1);
		info.setTelSecond1(zipphone2);
		info.setTelSecond2(ziptelphone2);

		info.setUrl1(zipurl1);
		info.setUrl2(zipurl2);
		info.setZipCode1(zip1);
		info.setZipCode2(zip2);
		int idd = ((Users) request.getSession().getAttribute("Users")).getUserid();
		if (txtCommycard != null && txtCommycard.equals("on")) {
			
			ObjectDao inf = new ObjectDao();
			int id = 0;
			if (mode == 0) {
				try {
					id = inf.add(info, ObjectDao.NOTINSERTID);
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				Mark mar = new Mark();

				mar.setOwuserid(idd);
				mar.setIspublic(ddlstPublic);
				mar.setCardid(id);
				mar.setCardfront(img1);
				mar.setCardback(img2);

				ObjectDao mara = new ObjectDao();
				try {
					mara.add(mar, ObjectDao.INSERTALL);
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				info.setCardid(upid);
				id=upid;
				try {
					inf.updates(info);
				} catch (SecurityException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IllegalAccessException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (InvocationTargetException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (NoSuchMethodException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				Mark mar = new Mark();

				mar.setOwuserid(idd);
				mar.setIspublic(ddlstPublic);
				mar.setCardid(upid);
				mar.setCardfront(img1);
				mar.setCardback(img2);

				ObjectDao mara = new ObjectDao();
				try {
					mara.updates(mar);
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			
			Userdao userdao = new Userdao();
			Users user = (Users) request.getSession().getAttribute("Users");
//			idd = ((Users) request.getSession().getAttribute("Users"))
//					.getUserid();
			user.setCardid(id);
			try {
				userdao.updates(user);
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			ObjectDao inf = new ObjectDao();
			int id = 0;
			if (mode == 0) {
				try {
					id = inf.add(info, ObjectDao.NOTINSERTID);
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				Mark mar = new Mark();
				mar.setOwuserid(idd);
				mar.setIspublic(ddlstPublic);
				mar.setCardid(id);
				mar.setCardfront(img1);
				mar.setCardback(img2);

				ObjectDao mara = new ObjectDao();
				try {
					mara.add(mar, ObjectDao.INSERTALL);
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} 
			else 
			{
				info.setCardid(upid);
				try {
					id = inf.updates(info);
				} catch (SecurityException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IllegalAccessException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (InvocationTargetException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (NoSuchMethodException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				Mark mar = new Mark();

				mar.setOwuserid(idd);
				mar.setIspublic(ddlstPublic);
				mar.setCardid(upid);
				mar.setCardfront(img1);
				mar.setCardback(img2);

				ObjectDao mara = new ObjectDao();
				try {
					mara.updates(mar);
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		if(mode==0)
			out.println("<script>alert(\"操作成功\");location.href=\"cbxpage/addcart.jsp\";</script>");
		else
			out.println("<script>alert(\"操作成功\");self.close();</script>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
